﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CommonLibrary.Other
{
    public class SortFile : IComparer
    {
        SortOption mso;
        public SortFile(SortOption so)
        {
            mso = so;
        }

        int IComparer.Compare(object a, object b)
        {
            FileInfo fa = (FileInfo)a;
            FileInfo fb = (FileInfo)b;


            switch (mso)
            {
                case SortOption.FileName:
                    return String.Compare(fa.Name, fb.Name, true);
                case SortOption.Extension:
                    return String.Compare(Path.GetExtension(fa.Name), Path.GetExtension(fb.Name), true);
                case SortOption.CreationTime:
                    return DateTime.Compare(fa.CreationTime, fb.CreationTime);
                case SortOption.LastWriteTime:
                    return DateTime.Compare(fa.LastWriteTime, fb.LastWriteTime);
                default:
                    break;
            }

            return 0;
        }
    }

    public enum SortOption
    {
        FileName,
        Extension,
        CreationTime,
        LastWriteTime
    }
}
